package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.common.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Good;
import com.example.demo.service.GoodService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import static com.example.demo.common.R.success;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 商品表(Good)表控制层
 *
 * @author makejava
 * @since 2025-08-26 16:13:29
 */
@Tag(name = "商品管理", description = "商品相关的CRUD操作")
@RestController
@RequestMapping("good")
public class GoodController {
    /**
     * 服务对象
     */
    @Resource
    private GoodService goodService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param good 查询实体
     * @return 所有数据
     */
    @Operation(summary = "分页查询所有商品数据", description = "根据条件分页查询商品列表")
    @Parameters({
        @Parameter(name = "current", in = ParameterIn.QUERY, description = "当前页码", required = false, schema = @Schema(type = "integer", defaultValue = "1")),
        @Parameter(name = "size", in = ParameterIn.QUERY, description = "每页显示数量", required = false, schema = @Schema(type = "integer", defaultValue = "10")),
        @Parameter(name = "name", in = ParameterIn.QUERY, description = "商品名称", required = false, schema = @Schema(type = "string")),
        @Parameter(name = "price", in = ParameterIn.QUERY, description = "商品价格", required = false, schema = @Schema(type = "number"))
    })
    @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @GetMapping
    public R selectAll(Page<Good> page, Good good) {
        return success(this.goodService.page(page, new QueryWrapper<>(good)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询单条商品数据", description = "根据商品ID获取商品详情")
    @Parameter(name = "id", in = ParameterIn.PATH, description = "商品ID", required = true, schema = @Schema(type = "integer"))
    @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.goodService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param good 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增商品数据", description = "添加新的商品信息")
    @ApiResponse(responseCode = "200", description = "新增成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @PostMapping
    public R insert(@RequestBody Good good) {
        return success(this.goodService.save(good));
    }

    /**
     * 修改数据
     *
     * @param good 实体对象
     * @return 修改结果
     */
    @Operation(summary = "修改商品数据", description = "更新商品信息")
    @ApiResponse(responseCode = "200", description = "修改成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @PutMapping
    public R update(@RequestBody Good good) {
        return success(this.goodService.updateById(good));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @Operation(summary = "删除商品数据", description = "批量删除商品")
    @Parameter(name = "idList", in = ParameterIn.QUERY, description = "商品ID列表", required = true, schema = @Schema(type = "array", implementation = Long.class))
    @ApiResponse(responseCode = "200", description = "删除成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.goodService.removeByIds(idList));
    }
}

